"""
steam峰值数据爬取
天津工业大学 大数据实训 第四组
杨玺锟
2020-1-10
"""

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import datetime

import pymysql


class SteamDataPipeline(object):
    def __init__(self):
        try:
            self.conn = pymysql.connect(host="188.131.167.236", port=3306, db="steam", user="python",
                                        passwd="Python1234..",
                                        charset="utf8")
            self.cmd = self.conn.cursor()
        except Exception as e:
            print(e)

    def process_item(self, item, spider):
        date = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        sql = 'INSERT INTO game_data VALUES(%s, %s, %s, %s, %s, %s, %s, %s)'
        param = [date, item['pubg'], item['csgo'], item['dota2'], item['gta5'], item['r6'], item['mhw'], item['total']]
        try:
            self.cmd.execute(sql, param)
            self.conn.commit()
        except Exception as e1:
            print(e1)

    def close_spider(self, spider):
        self.cmd.close()
        self.conn.close()


